package com.dq.educms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dq.educms.entity.CrmBanner;
import com.dq.educms.service.CrmBannerService;
import com.dq.educms.mapper.CrmBannerMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dq.educms.vo.BannerVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页banner表(CrmBanner)表服务实现类
 *
 * @author Dq
 * @since 2022-08-04 17:56:08
 */
@Service
public class CrmBannerServiceImpl  extends ServiceImpl<CrmBannerMapper, CrmBanner>  implements CrmBannerService {
    @Autowired
    private CrmBannerMapper bannerMapper;

    @Override
    public List<BannerVo> getBannerList() {
        /**
         * 1. 获取所有Banner数据
         * 2. 封装为Vo对象
         * @return bannerVoList
         * @author Dq
         */
        LambdaQueryWrapper<CrmBanner> crmBannerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        crmBannerLambdaQueryWrapper.orderByDesc(CrmBanner::getId).last("limit 2");
        List<CrmBanner> crmBanners = bannerMapper.selectList(crmBannerLambdaQueryWrapper);
        List<BannerVo> bannerVoList = copyList(crmBanners);
        return bannerVoList;
    }

    /**
     * 单个Banner拷贝Vo
     * @param crmBanner
     * @return
     */
    private BannerVo copy(CrmBanner crmBanner){
        BannerVo bannerVo = new BannerVo();
        BeanUtils.copyProperties(crmBanner,bannerVo);
        return bannerVo;
    }

    /**
     * 多个封装Vo对象
     * @param crmBannerList
     * @return
     */
    private List<BannerVo> copyList(List<CrmBanner> crmBannerList){
        ArrayList<BannerVo> bannerVos = new ArrayList<>();
        for (CrmBanner banner : crmBannerList) {
            bannerVos.add(copy(banner));
        }
        return bannerVos;
    }
}